package com.aso114.project.mvp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.adapter.ChoiceExamAdapter;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.bean.CityInfoBean;
import com.aso114.project.mvp.LoginSP;
import com.aso114.project.mvp.activity.fragment.AdministrarAptitudeTestFragment;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Helper;
import com.aso114.project.util.SpfUtil;
import com.aso114.project.util.SystemBarHelper;
import com.aso114.project.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 选择考区
 */
public class ChoiceExamActivity extends BaseSimpleActivity {
    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    @BindView(R.id.examlist)
    ListView examlist;
    List<String> datas;
    ChoiceExamAdapter adapter;
    @BindView(R.id.layout_title_bar_right_tv)
    TextView layoutTitleBarRightTv;
    public static String cityid = "";
    public static String cityname = "";
    public static String time = "";
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;

    @Override
    protected int getResouseId() {
        return R.layout.choiceexam;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(this);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(this, 0);
            SystemBarHelper.setStatusBarDarkMode(this);
        }
        layoutTitleBarTitleTv.setText("切换考区");
        layoutTitleBarRightTv.setText("保 存");
//        getData();


        CommentModel.getInstant().getCityInfo(new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {

                Helper.gotoLogin(isSuccess);//token失效去重新登陆
                final List<CityInfoBean> parkData = (List<CityInfoBean>) o;


                adapter = new ChoiceExamAdapter(ChoiceExamActivity.this, parkData);
                examlist.setAdapter(adapter);
                examlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {


                        adapter.setChecked(position);//传入现在选择的position 现在用到了
                        adapter.notifyDataSetInvalidated();//重绘
                        // 加入逻辑代码
                        cityid = parkData.get(position).getAreaId();

                        cityname = parkData.get(position).getAreaName();

                        time = parkData.get(position).getExamTime();

                    }
                });

            }

            @Override
            public void onFailure(String fail) {

            }
        });


    }

    public void getData() {
        datas = new ArrayList<String>();

        datas.add("国考");
        datas.add("安微省考");
        datas.add("北京市考");
        datas.add("重庆市考");
        datas.add("福建省考");
        datas.add("甘肃省考");
        datas.add("广东省考");
        datas.add("河北省考");
        datas.add("河南省考");
        datas.add("安微省考");
        datas.add("北京市考");
        datas.add("重庆市考");
        datas.add("福建省考");
        datas.add("甘肃省考");
        datas.add("广东省考");
        datas.add("河北省考");
        datas.add("河南省考");


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_title_bar_back_iv, R.id.layout_title_bar_right_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_title_bar_back_iv:
                finish();
                break;
            case R.id.layout_title_bar_right_tv:

                if (cityid.equals("")) {
                    ToastUtils.showToast(this, "请选择考区");
                } else {

                    CommentModel.getInstant().get_save_arid(cityid, new CallBack() {
                        @Override
                        public void onSuccess(Object o, boolean isSuccess, String msg) {
                            Helper.gotoLogin(isSuccess);//token失效去重新登陆
                            LoginSP.getInstance().setUserAreaId(cityid);
                            LoginSP.getInstance().setCityname(cityname);
                            SpfUtil.getInstance().putString("dayTime",Helper.getCurrentTime(Long.valueOf(time)));
                            Intent intent = new Intent();
                            intent.setAction(AdministrarAptitudeTestFragment.HOME_REFRESH);
                            sendBroadcast(intent);
                            finish();
                        }

                        @Override
                        public void onFailure(String fail) {
                        }
                    });
                }

                break;
        }
    }
}
